import { SUCCESS_CODE } from '@/constants'

const timeout = 1000

const List: {
  username: string
  password: string
  role: string
  roleId: string
  permissions: string | string[]
  userType: number
  token: string
}[] = [
  {
    username: 'admin',
    password: 'admin',
    role: 'admin',
    roleId: '1',
    permissions: ['*.*.*'],
    userType: 0,
    token: 'admin'
  },
  {
    username: 'test',
    password: 'test',
    role: 'test',
    roleId: '2',
    permissions: ['example:dialog:create', 'example:dialog:delete'],
    userType:1,
    token: 'test'
  }
]

export default [
  // 列表接口
  {
    url: '/api/user/list',
    method: 'get',
    response: ({ query }) => {
      const { username, pageIndex, pageSize } = query

      const mockList = List.filter((item) => {
        if (username && item.username.indexOf(username) < 0) return false
        return true
      })
      const pageList = mockList.filter(
        (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
      )

      return {
        code: SUCCESS_CODE,
        data: {
          total: mockList.length,
          list: pageList
        }
      }
    }
  },
  // 登录接口
  {
    url: '/api/auth/login',
    method: 'post',
    timeout,
    response: ({ body }) => {
      const data = body
      let hasUser = false
      for (const user of List) {
        if (user.username === data.username && user.password === data.password) {
          hasUser = true
          return {
            code: SUCCESS_CODE,
            data: user
          }
        }
      }
      if (!hasUser) {
        return {
          code: 500,
          message: '账号或密码错误'
        }
      }
    }
  },
  // 退出接口
  {
    url: '/api/auth/logout',
    method: 'post',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  },
  // 注册接口
  {
    url: '/api/user/insert',
    method: 'post',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  },
  // 修改接口
  {
    url: '/api/user/list',
    method: 'post',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: {
          totalCount: 2,
          totalPage: 1,
          pageSize: 10,
          list: [
            {
              userId: 1,
              username: 'fuyongqi',
              password: 'fuyongqi',
              name: '傅永奇',
              userType: 0,
              email: 'fuyongqi@163.com',
              userStatus: 1,
              createTime: '2025-01-28 14:59:48',
              updateTime: '2025-01-28 14:59:48'
            },
            {
              userId: 2,
              username: 'fuyongde',
              password: 'fuyongde',
              name: '傅永德',
              userType: 1,
              email: 'fuyongde@163.com',
              userStatus: 1,
              createTime: '2025-01-28 15:00:00',
              updateTime: '2025-01-28 15:00:00'
            },
            {
              userId: 3,
              username: 'chenmiaoshan',
              password: 'chenmiaoshan',
              name: '陈妙珊',
              userType: 1,
              email: 'chenmiaoshan@163.com',
              userStatus: 0,
              createTime: '2025-01-28 15:00:00',
              updateTime: '2025-01-28 15:00:00'
            }
          ]
        }
      }
    }
  },
  // 启用
  {
    url: '/api/user/enable',
    method: 'post',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  },
  {
    url: '/api/user/disable',
    method: 'post',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  }
]
